const querystring=require('querystring')
const handleUserRouter=require('./src/routes/user')


const serverHandler =(req,res)=>{
    res.setHeader('Content-Type','application/json');
    const method=req.method;
    const url=req.url;
    req.path=url.split('?')[0];
    req.query=querystring.parse(url.split('?')[1]);

    req.cookie={};
    const cookieStr=req.headers.cookie||'';
    cookieStr.split(';').forEach(item=>{
        if(!item){
            return
        }
        const arr=item.split('=')
        const key=arr[0].trim()//trim()方法去掉前后空格
        const val=arr[1].trim()
        req.cookie[key]=val;
    })

    const userResult = handleUserRouter(req,res)
    if(userResult){
        res.end(
            JSON.stringify(userResult)
        )
        return
    }

    res.end(JSON.stringify(req.cookie))
}

module.exports=serverHandler